<?php
/**
 * Author      : rainbow <hzboye010@163>
 * DateTime    : 2016-12-23 09:29:37
 * Description : [支付回调处理]
 */

namespace app\index\controller;

use app\src\consumehistory\logic\ConsumeHistoryLogic;
use app\src\hook\PaySuccessHook;
use app\src\order\enum\PayCurrency;
use app\src\order\enum\PayType;
use app\src\order\logic\OrdersLogic;
use app\src\payment\action\PaymentAction;
use app\src\payment\enum\PayEnum;
use app\src\profit\action\ProfitAction;
use app\src\profit\enum\ProfitEnum;
use app\src\repair\logic\RepairLogicV2;
use app\src\wallet\logic\WalletLogic;
use app\src\wxpay\logic\WxpayNotifyLogicV2;
use app\src\wxpay\service\WxpayService;
use app\src\wxpayApp\service\WxpayServiceApp;
use think\Controller;
use think\Db;

/**
 * [simple_description]
 *
 * [detail]
 *
 * @author  rainbow <hzboye010@163>
 * @package app\
 * @example
 */
class Payback extends Controller{
    const WEIXIN = 3;
    const ALIPAY = 1;

    //模拟支付完成 - test only
    public function test(){
        $result  = array();
        $now     = time();
        $service = new WxpayService();

        //test only
        $pay_type = isset($_GET['pay_type']) ? (int) $_GET['pay_type'] : $this->exits('pay_type');
        $pay_code = isset($_GET['pay_code']) ? $_GET['pay_code'] : $this->exits('pay_code');
        $fee      = isset($_GET['fee']) ? max(intval($_GET['fee']),1) : 1;
        if(!in_array($pay_type,[self::WEIXIN,self::ALIPAY])) $this->exits('invalid pay_type');

        $data = $this->getTestData($pay_type,$pay_code,$fee);
        if($pay_type === self::WEIXIN){
            $this->exits('微信支付已接入,请使用1分钱真实支付');
            // $trade_status = $data['result_code'];//交易状态
            // addLog('Payback/wxpay','parsed data',$data,'微信支付回调:模拟');
            // if ($trade_status == 'SUCCESS') {
            //   //支付成功
            //   Db::startTrans();
            //   $order = $this->paySuccess($data);
            //   $this->addWeixinNotify($data,$order);
            //   Db::commit();
            //   $result['return_code'] = "SUCCESS : ".$order;
            // }
            // exit(dump($result));
        }elseif($pay_type === self::ALIPAY){
            $this->exits('暂未实现或不在此处');
        }else{
            $this->exits('unsupported pay_type');
        }
    }
    //获取 微信支付回调 模拟数据 - test only

    private function exits($str = '')
    {
        exit('nothing to say : ' . $str);
    }

    //微信支付回调

    private function getTestData($pay_type=0,$pay_code='',$fee = 1){
        if (empty($pay_code)) $pay_code = 'SELL2017062015P4';
        //PA1635615280173426951  - 商城
        //WX163561533568875476V  - 维修
        //WC16363112222557575201 - 余额充值

        if($pay_type === self::WEIXIN){
            return [
                "appid"          =>"wxbdcf4725f2e1968f",
                "bank_type"      =>"CFT",
                "cash_fee"       =>"".$fee,
                "fee_type"       =>"CNY",
                "is_subscribe"   =>"N",
                "mch_id"         =>"1294387501",
                "nonce_str"      =>"yn7LWRWZRooP2wBNJBHXOl4vPneUX1VW",
                "openid"         =>"oBuUFwSyN0j2yNZDHVKsXTHW7tFI",
                "out_trade_no"   =>$pay_code,
                "result_code"    =>"SUCCESS",
                "return_code"    =>"SUCCESS",
                "sign"           =>"78C4204ED01AA0161FD344EE6A250FB9",
                "time_end"       =>"20160702093808",
                "total_fee"      =>"".$fee,
                "trade_type"     =>"APP",
                "transaction_id" =>"4010162001201607028225413779"
            ];
        }elseif($pay_type === self::ALIPAY){
            return [];
        }else{
            return [];
        }
    }

    public function wxpay(){

        $result  = array();
        $now     = time();
        $service = new WxpayService();
        $test = false;// ? 测试 & 验证签名
        if($test){
            $data = $this->getTestData(self::WEIXIN, '');
        }else{
            //获取回调信息
            $data = file_get_contents('php://input');
            $streamData = empty($data)?'':$data;
            if(empty($streamData)){
                addLog('Payback/wxpay','parsed data','no data','微信支付回调');
                exit('nothing to say');
            }

            $data = $service->xmlstr_to_array($streamData);
        }
        addLog('Payback/wxpay','parsed data',$data,'微信支付回调');
        addLog('Payback/wxpay','parsed data',$service->checkSign($data),'微信验证签名结果');
        if($test || $service->checkSign($data)) {
            $trade_status = $data['result_code'];//交易状态
            //签名验证成功
            if ($trade_status == 'SUCCESS') {
                //支付成功

                //订单支付成功
                $result = $this->paySuccess($data,PayType::WXPAY);
                addLog('Payback/wxpay','parsed data',$result,'处理');
//                $payment=(new PaymentAction())->addPayment(2,$data['total_fee'],$data['out_trade_no'],$data['transaction_id'],'1');
//                if($payment['status']){
//                    $consume_history=(new ConsumeHistoryLogic())->seller_history(($data['out_trade_no']));
//                    $profit_amount = $payment['info']['profit_amount'];
//                    if($profit_amount<100){
//                        $profit_deal=['status'=>true];
//                    }else{
//                        $profit_deal = (new ProfitAction())->dealProfit(ProfitEnum::SELLER_TYPE,$consume_history['info'],$profit_amount,$payment['info']['order_id']);
//                    }
//                }
                //写入第三方支付信息
                //$this->addWeixinNotify($data,$order);
                if($result['info']=='success'){

                    $result['return_code'] = "SUCCESS";
                }else{

                    $result['return_code'] = "FAIL";
                }



            }
        } else {
            $sign=$data['sign'];
            addLog('Payback/wxpay','签名验证失败',$sign,'微信支付回调');
            $payment=(new PaymentAction())->addPayment(2,'0',$data['out_trade_no'],$data['transaction_id'],'2');
            //验证失败
            $result['return_code'] = "FAIL";
            $result['return_msg']  = '签名失败';
            //调试用，写文本函数记录程序运行情况是否正常
            //logResult("这里写入想要调试的代码变量值，或其他运行的结果记录");
        }
        exit($service->arrayToXml($result));
    }

    public function wxpayApp(){
        $result  = array();
        $now     = time();
        $service = new WxpayServiceApp();
        $test = false;// ? 测试 & 验证签名
        if($test){
            $data = $this->getTestData(self::WEIXIN, '');
        }else{
            //获取回调信息
            $data = file_get_contents('php://input');
            $streamData = empty($data)?'':$data;
            if(empty($streamData)){
                addLog('Payback/wxpayApp','parsed data','no data','微信支付回调');
                exit('nothing to say');
            }

            $data = $service->xmlstr_to_array($streamData);
        }
        addLog('Payback/wxpayApp','parsed data',$data,'微信支付回调');
        addLog('Payback/wxpayApp','parsed data',$service->checkSign($data),'微信验证签名结果');
        if($test || $service->checkSign($data)) {
            $trade_status = $data['result_code'];//交易状态
            //签名验证成功
            if ($trade_status == 'SUCCESS') {
                //支付成功

                //订单支付成功
                $result = $this->paySuccess($data,PayType::WXPAYAPP);
                addLog('Payback/wxpayApp','parsed data',$result,'处理');
//                $payment=(new PaymentAction())->addPayment(2,$data['total_fee'],$data['out_trade_no'],$data['transaction_id'],'1');
//                if($payment['status']){
//                    $consume_history=(new ConsumeHistoryLogic())->seller_history(($data['out_trade_no']));
//                    $profit_amount = $payment['info']['profit_amount'];
//                    if($profit_amount<100){
//                        $profit_deal=['status'=>true];
//                    }else{
//                        $profit_deal = (new ProfitAction())->dealProfit(ProfitEnum::SELLER_TYPE,$consume_history['info'],$profit_amount,$payment['info']['order_id']);
//                    }
//                }
                //写入第三方支付信息
                //$this->addWeixinNotify($data,$order);
                if($result['info']=='success'){

                    $result['return_code'] = "SUCCESS";
                }else{

                    $result['return_code'] = "FAIL";
                }



            }
        } else {
            $sign=$data['sign'];
            addLog('Payback/wxpayApp','签名验证失败',$sign,'微信支付回调');
            $payment=(new PaymentAction())->addPayment(2,'0',$data['out_trade_no'],$data['transaction_id'],'2');
            //验证失败
            $result['return_code'] = "FAIL";
            $result['return_msg']  = '签名失败';
            //调试用，写文本函数记录程序运行情况是否正常
            //logResult("这里写入想要调试的代码变量值，或其他运行的结果记录");
        }
        exit($service->arrayToXml($result));
    }
    //写入微信回调记录

    /**
     * 各类订单 - 微信支付成功处理 - 外部请加事务
     * @Author
     * @DateTime 2016-12-29T15:31:55+0800
     * @param    array                    $data [description]
     * @return   [type]                         [description]
     */
    private function paySuccess(array $data,$PayType){

        $total_fee=$data['total_fee'];
        $out_trade_no=$data['out_trade_no'];
        $trade_no=$data['transaction_id'];
        $seller_id='111';
        //1. 对支付宝默认其货币为: 人民币：元，所以 * 100 转换为分
        $result =  (new PaySuccessHook())->success($seller_id,$total_fee * 100,$out_trade_no,PayCurrency::RMB,$trade_no,$PayType);

        return $result;


//        $now = time();
//
//        $out_trade_no = $data['out_trade_no'];
//        $pay_code     = $out_trade_no;//本地支付单号
//        $trade_no     = $data['transaction_id'];
//        $total_fee    = $data['total_fee'];//
//
//        $order_type = substr($pay_code,0,2);
//        if($order_type == 'PA'){         //商城订单
//            $r = (new OrdersLogic())->paySuccessCall($pay_code,$total_fee,self::WEIXIN,$trade_no);
//        }elseif($order_type == 'WX'){    //维修订单
//            $r = (new RepairLogicV2())->paySuccess($pay_code,$total_fee,self::WEIXIN,$trade_no);
//        }elseif($order_type == 'WC'){    //余额充值
//            $r = (new WalletLogic())->paySuccess($pay_code,$total_fee,self::WEIXIN,$trade_no);
//        }else{
//            return '未知支付码['.$pay_code.']';
//        }
//        return $r['status'] ? $r['info']['msg'] : $r['info'];
    }

    //private log

    private function addWeixinNotify($data,$order){
        $his = [
            'sign'           => $data['sign'],
            'openid'         => $data['openid'],
            'trade_type'     => $data['trade_type'],
            'bank_type'      => $data['bank_type'],
            'total_fee'      => $data['total_fee'],
            'cash_fee'       => $data['cash_fee'],
            'transaction_id' => $data['transaction_id'],
            'out_trade_no'   => $data['out_trade_no'],
            'time_end'       => $data['time_end'],
            'extra'          => $order.';支付成功',
        ];
        //写入微信回调记录
        (new WxpayNotifyLogicV2())->add($his);
    }
}